<?php
namespace Lib;

class Net {
	
	function __construct() {
	
	}
	
	function __destruct() {
	
	}
	
	/**
	 * ping
	 *
	 * @param 需要ping的地址 $sAddress
	 * @param ping的次数默认3次 $iTimes
	 * @return boolean multitype:NULL
	 */
	public function ping($sAddress, $iTimes = 3) {
		switch (PHP_OS) {
			case 'FreeBSD' :
			case 'Linux' :
				$sResult = shell_exec ( 'ping -c ' . $iTimes . ' ' . $sAddress );
				break;
		}
		switch (PHP_OS) {
			case 'FreeBSD' :
				if (preg_match ( '/([\d\.]+)\% packet loss/i', $sResult, $aMatch )) {
					$iLoss = $aMatch [1];
					if ($iLoss == 100) {
						return false;
					}
					$aResult = array (
							'loss' => $iLoss 
					);
					if ($iLoss < 100) {
						if (preg_match_all ( '/time=([\d\.]+) ms/', $sResult, $aMatch )) {
							sort ( $aMatch [1], SORT_NUMERIC );
							$aResult ['min'] = $aMatch [1] [0];
							$aResult ['max'] = $aMatch [1] [$iTimes - 1];
						}
					}
				}
				break;
		}
		return $aResult;
	}
}
?>